var mongoose = require('mongoose'),
 　 db = mongoose.connection;  //不是connect

mongoose.connect('mongodb://localhost/learn');

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open',function(callback){

    var kittySchema = mongoose.Schema({  //Schema 框架　骨架
        name:String,
    });

    kittySchema.methods.speak = function(){
        var greeting = this.name
        ? "Meow name is " + this.name
        : "I don't have a name"

        console.log(greeting); 
    }

    var kitten = mongoose.model('kitten',kittySchema); //model 模型

    var silenc = new kitten({name:'Silenc'});
    var fluffy = new kitten({name:'fluffy'});

    fluffy.save(function(err,fluffy){                 //从model的实例层级 调用save方法保存数据至learn数据库 只能用实例调用而不用model 
        if(err) return console.error(err);
        fluffy.speak();
    });

    kitten.find({name:'fluffy'},function(err,data){ //从model层级调用find()方法查找数据
        if(err) return console.error(err);

        /*
        data[2].name = "nihao";
        data[1].save(function(err){                  //修改数据，经过自己尝试这样是不可以的，这样只能暂时修改数据
           if (err) return handleError(err);
       })
        console.log(data[1]._id);
        console.log(data);
        */
    });

    console.log(silenc.name); 

})